<?php

abstract class AbstractBackendViewController implements IViewController
{
    /**
     *
     * @var BackendApplicationContext
     */
    protected $applicationContext;

    /**
     *
     * @var string
     */
    protected $permissionSubject;

    /**
     *
     * @var Request
     */
    protected $request;

    /**
     *
     * @var Response
     */
    protected $response;

    /**
     *
     * @var View
     */
    protected  $view;

    /**
     *
     * @var ViewModel
     */
    protected $viewModel;

    /**
     *
     */
    public function __construct(BackendApplicationContext $applicationContext)
    {
        $this->applicationContext = $applicationContext;
        return $this;
    }

    public function handleRequest(Request $request, Response $response, View $view) {

        $this->request = $request;
        $this->response = $response;
        $this->view = $view;

        $this->viewModel = new ViewModel($this->applicationContext->getMenu());

        $this->applicationContext->getEventHandler()->dispatchEvent(Constants::EVENT_VIEW_PRE_RENDER, $this->viewModel);

        $sessionUser = $this->applicationContext->getSessionUser();

        $view->setVar('hasEditPermission', $sessionUser->hasPermission($this->permissionSubject, Constants::ACCESS_LEVEL_VIEW_EDIT))
             ->setVar('hasCreatePermission', $sessionUser->hasPermission($this->permissionSubject, Constants::ACCESS_LEVEL_VIEW_EDIT_CREATE))
             ->setVar('hasDeletePermission', $sessionUser->hasPermission($this->permissionSubject, Constants::ACCESS_LEVEL_VIEW_EDIT_CREATE_DELETE))
             ->setVar('viewModel', $this->viewModel)
             ->setVar('currentAdministrator', $this->applicationContext->getCurrentAdministrator())
             ->setLayout('system');
    }
}